package com.demo.compute;

import java.util.ArrayList;
import java.util.List;

public class DiffWaysToCompute {
    public static void main(String[] args) {
        String b = "2-1-1";
        List<Integer> integers = diffWaysToCompute(b);
        for (Integer integer : integers) {
            System.out.println(integer);
        }
    }


    private static List<Integer> diffWaysToCompute(String input) {
        List<Integer> ways = new ArrayList<>();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '+' || c == '-' || c == '*') {
                List<Integer> left = diffWaysToCompute(input.substring(0, i));
                List<Integer> right = diffWaysToCompute(input.substring(i + 1));
                for (Integer a : left) {
                    for (Integer b : right) {
                        switch (c) {
                            case '+':
                                ways.add(a + b);
                                break;
                            case '-':
                                ways.add(a - b);
                                break;
                            case '*':
                                ways.add(a * b);
                                break;
                        }
                    }
                }
            }
            if (ways.size() == 0)
                ways.add(Integer.valueOf(input));
        }
        return ways;
    }
}
